package io.mft.datasource.ar.executor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.mft.api.domain.DataAccessRegulation;
import io.mft.api.domain.DataType;
import io.mft.api.domain.ExecutingResource;
import io.mft.as.ar.gateway.client.Client;
import io.mft.as.ar.gateway.result.Result;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMapList;
import io.mft.dev.base.BaseFunctionOfDataAccess;
import io.mft.plugin.PAFunction;
import io.mft.plugin.PluginContext;
/**
 * 业务中间件功能号调用
 * @说明：TODO
 * @类型名称：ArFunctionExecutor
 * @创建者: 胡常建  
 * @创建时间: 2021年3月30日 下午1:29:41
 * @修改者: 胡常建  
 * @修改时间: 2021年3月30日 下午1:29:41
 */
@PAFunction(value = "ar.arFuncNo.pagelist.DataAccessProcessExecutor",note = "AS路由功能号调用")
public class ArFunctionPageListExecutor extends BaseFunctionOfDataAccess{
	Logger logger=LoggerFactory.getLogger(getClass());
	@Override
	public ExecuteReturn doExecute(PluginContext<ExecutingResource<DataAccessRegulation>> context) {
		DataAccessRegulation dataAccessRegulation=context.getValue().getExecutingRegulation();
		String v=dataAccessRegulation.getValue();
		int funcNo=Integer.valueOf(v);
		Client client=new Client(dataAccessRegulation.getDatasource().getKey());
		logger.info("调用AR路由服务:{}-功能号:{}-参数:{}",dataAccessRegulation.getDatasource().getKey(),funcNo,context.getRequest().getRequestValue());
		Result result=client.invokeToResult(funcNo, context.getRequest().getRequestValue());
		logger.info("调用AR路由服务:{}-功能号:{}-参数:{},返回状态:{},返回信息:{}",dataAccessRegulation.getDatasource().getKey(),funcNo,context.getRequest().getRequestValue(),result.getErr_no(),result.getErr_info());
		
		context.getResponse().setErrorInfo(result.getErr_info());
		context.getResponse().setErrorNo(result.getErr_no());
		if (result.getErr_no()==0) {
			result.getDsNameArr().forEach(ds->{
				if (dataAccessRegulation.getDataType()==DataType.pagelist) {
					context.getResponse().result(ds.getName(), result.getPage(ds.getName()));
					logger.info("调用AR路由服务:{}-功能号:{}-参数:{},返回状态:{},返回信息:{},返回数据:{}",dataAccessRegulation.getDatasource().getKey(),funcNo,context.getRequest().getRequestValue(),result.getErr_no(),result.getErr_info(),result.getPage(ds.getName()).getList());
				}
			});
		}
		return ExecuteReturn.builder().build();
	}

	
}
